import axios from "axios";

axios.defaults.withCredentials = true
import {Notification, MessageBox} from 'element-ui'
import router from '../router'
import {GetCookie, RemoveCookie, setnullString} from "../config/cookie";
axios.interceptors.request.use(config => {
    console.log(config)//发送给后端的请求体
    if (!config.notToken) {
        if (GetCookie("token")) {
            // 用户已经登录
            config.headers["Authorization"] = GetCookie("token");
        }
    }
    return config
})
axios.interceptors.response.use(
    res => {
        console.log(res)
        if (res.status == 200 || res.status == 201 || res.status == 204) {
            return setnullString(res.data);
        }
        // 否则返回 res，保证下一个拦截器获得正确的返回值
        return res;
    },
    err => {
        // 处理请求错误
        if (err.response.status == 401) {
            MessageBox.alert('登录状态已过期，请您重新登录', '系统提示', {
                confirmButtonText: '确定',
                callback: action => {
                    RemoveCookie('token');
                    router.push("/login");
                }
            });
        }
        // if (err.response.status == 400) {
        //     router.push('*')
        // }
        if (err.response.status == 500) {
            Notification.error({
                title: '错误',
                message: '接口请求失败'
            });
        }
        return Promise.reject(err)
    });
export default axios